package driver;

import inventory.view.InventoryMainView;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import javax.swing.JFrame;
import javax.swing.JOptionPane;

import crm.view.CrmMainView;

import projectincharge.view.ProjectInchargeMainView;
import purchasedepartment.view.PurchaseDepartmentMainView;
import qualityengineer.view.QualityEngineerMainView;

import siteengineer.view.SiteEngineerMainView;

import dbhandelling.Connections;

public class LoginViewController implements ActionListener
{
	LoginView lv;
	Statement stmnt;
	JFrame frame1;
	
	public LoginViewController(LoginView lv,JFrame frame)
	{
		this.lv=lv;
		this.frame1=frame;
		lv.btnActionListener(this);
		
		Connections.getMyConnection();
		stmnt=Connections.getMyStatement();
		System.out.println(stmnt+"val");
		
	}
	
	@Override
	public void actionPerformed(ActionEvent arg0) 
	{
		String userName,password;
		
		userName=lv.getTextFieldUserName().getText();
		password=lv.getTextFieldPassword().getText();
		
		String sql="select authorityid from logintable where username="+"'"+userName+"'"+"and password="+"'"+password+"'";                             
		try 
		{
			//System.out.println("query :"+sql);
			ResultSet rs=stmnt.executeQuery(sql);
			
			rs.next();
			if(rs.getInt(1)==1)
			{
				frame1.getContentPane().remove(0);
				frame1.getContentPane().add(new InventoryMainView());
				frame1.setVisible(true);
				frame1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
				frame1.setExtendedState(java.awt.Frame.MAXIMIZED_BOTH);
			}
			else if(rs.getInt(1)==2)
			{
				frame1.getContentPane().remove(0);
				frame1.getContentPane().add(new SiteEngineerMainView());
				frame1.setVisible(true);
				frame1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
				frame1.setExtendedState(java.awt.Frame.MAXIMIZED_BOTH);
			}
			else if(rs.getInt(1)==3)
			{
				frame1.getContentPane().remove(0);
				frame1.getContentPane().add(new PurchaseDepartmentMainView());
				frame1.setVisible(true);
				frame1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
				frame1.setExtendedState(java.awt.Frame.MAXIMIZED_BOTH);
			}
			else if(rs.getInt(1)==4)
			{
				frame1.getContentPane().remove(0);
				frame1.getContentPane().add(new ProjectInchargeMainView());
				frame1.setVisible(true);
				frame1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
				frame1.setExtendedState(java.awt.Frame.MAXIMIZED_BOTH);
			}
			else if(rs.getInt(1)==5)
			{
				frame1.getContentPane().remove(0);
				frame1.getContentPane().add(new QualityEngineerMainView());
				frame1.setVisible(true);
				frame1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
				frame1.setExtendedState(java.awt.Frame.MAXIMIZED_BOTH);
			}
			else if(rs.getInt(1)==6)
			{
				
				crm.dbhandelling.Connections.loadDriver();
				crm.dbhandelling.Connections.connect();
				frame1.getContentPane().remove(0);
				frame1.getContentPane().add(new CrmMainView());
				frame1.setVisible(true);
				frame1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
				frame1.setExtendedState(java.awt.Frame.MAXIMIZED_BOTH);
			}
			
			
			else
			{
				JOptionPane.showMessageDialog(lv, "Username or password doesn't match...!");
			}
		} 
		catch (SQLException e) 
		{
			e.printStackTrace();
		}
	}
	
}
